//
//  starView.m
//  jiajiao
//
//  Created by apple on 16/4/15.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "starView.h"

@interface starView ()
@property(nonatomic,strong)UIView *orgenview;
@property(nonatomic,strong)NSMutableArray *array;
@end

@implementation starView
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
-(instancetype)initWithFrame:(CGRect)frame
{NSLog(@"33");
    if (self = [super initWithFrame:frame]) {
        [self changeUI];
    }
    return self;
}
//-(instancetype)init
//{
//    if (self=[super init]) {
//        [self changeUI];
//    }
//    return self;
//}

-(void)changeUI
{
    self.backgroundColor=[UIColor blackColor];
    self.orgenview = [[UIView alloc]init];
    self.orgenview.backgroundColor = [UIColor orangeColor];
    [self addSubview:self.orgenview];
    self.startcount=5;
    [self addimageview];

}
-(void)addimageview

{
    
    
    NSLog(@"%ld",(long)self.startcount);
    
    
    for (NSInteger i=0; i<self.startcount; i++) {
        UIImageView *imageview =[[UIImageView alloc]init];
        imageview.userInteractionEnabled = NO;
        imageview.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageclic:)];
        [imageview addGestureRecognizer:tap];
        [self addSubview:imageview];
        imageview.backgroundColor =[UIColor clearColor];
        [self.array addObject:imageview];
    }

}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width =self.width/self.startcount;
    for (NSInteger i =0; i<self.self.array.count; i++) {
        UIImageView *imview =self.array[i];
        imview.image= [UIImage imageNamed:@"star1.png"];
        imview.frame = CGRectMake(width*i, 0, width, width);
    }
    
}
-(void)imageclic:(UITapGestureRecognizer *)tap
{
    CGFloat width =self.width/self.startcount;

    self.orgenview.frame=CGRectMake(0, 0,width*(tap.view.tag+1) , self.height);
    
    [self.deleget startCount:tap.view.tag+1];

}
-(void)setUserenable:(BOOL)userenable
{
    _userenable = userenable;
    for (NSInteger i =0; i<self.array.count; i++) {
        UIImageView *imview =self.array[i];
        imview.userInteractionEnabled = userenable;
    }

}
-(void)showStar:(CGFloat)star
{
    
    
    NSInteger newstar = (int)star;
    CGFloat width =self.width/self.startcount;
    
    CGFloat xiaoshu = star-newstar;
    if (xiaoshu==0) {
    self.orgenview.frame=CGRectMake(0, 0,width*star, self.height);

    }else
    {
     float dayu = (3.0/40+(17.0/20)*xiaoshu+newstar)*width;
    self.orgenview.frame=CGRectMake(0, 0,dayu, self.height);


    }
    
    
}
-(void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    self.backgroundColor=normalColor;

}
-(void)setSelectColor:(UIColor *)selectColor
{
    _selectColor = selectColor;
    self.orgenview.backgroundColor = selectColor;
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    UITouch*touch = [touches anyObject];
//    
//    CGPoint touchLocation = [touch locationInView:self];
//
//    self.orgenview.frame=CGRectMake(0, 0, touchLocation.x, self.height);
//
//}
//-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    UITouch*touch = [touches anyObject];
//    
//    CGPoint touchLocation = [touch locationInView:self];
//    
//    self.orgenview.frame=CGRectMake(0, 0, touchLocation.x, self.height);
//    
//
//}
//-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    UITouch*touch = [touches anyObject];
//    
//    CGPoint touchLocation = [touch locationInView:self];
//    
//    self.orgenview.frame=CGRectMake(0, 0, touchLocation.x, self.height);
//    
//
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
